package demo

import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.webkit.ValueCallback
import com.zalyyh.game.game.layaair.game.IMarket.IPluginRuntimeProxy

class RuntimeProxy(mainActivity: Activity) : IPluginRuntimeProxy {

    private val TAG = "RuntimeProxy"
    private var mActivity: Activity? = null
    val cacheDir: String
        get() {
            val sCache = mActivity!!.cacheDir.toString()
            val vString = sCache.split("/".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
            var sNewCache = ""
            for (i in 0 until vString.size - 1) {
                sNewCache += vString[i]
                sNewCache += "/"
            }
            return sNewCache
        }
    //TODO CP自行定制
    val expansionMainPath: String
        get() = ""
    //TODO CP自行定制
    val expansionPatchPath: String
        get() = ""

    init {
        mActivity = mainActivity
    }

   override fun laya_set_value(key: String, value: Any): Boolean {
        // TODO Auto-generated method stub
        return false
    }

    override fun laya_get_value(key: String): Any? {
        Log.d(TAG, "laya_get_value key=$key")
        val str: String? = null
        if (key.equals("CacheDir", ignoreCase = true)) {
            return cacheDir
        } else if (key.equals("ExpansionMainPath", ignoreCase = true)) {
            return expansionMainPath
        } else if (key.equals("ExpansionPatchPath", ignoreCase = true)) {
            return expansionPatchPath
        }
        return str
    }

    override fun laya_stop_game_engine() {
        // TODO Auto-generated method stub
        Log.d(TAG, "Login laya_stop_game_engine.")
    }

    override fun laya_invoke_Method(method: String, param: Bundle): Any? {
        // TODO Auto-generated method stub
        return null
    }

    override fun Login(jsonObj: JSONObject, callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Login info = $jsonObj")
        try {
            val result = JSONObject()
            result.put("status", 0)
            result.put("unionUserID", "1123fff4")
            result.put("nickName", "中南海")
            result.put("photo", "http://www.xxx.com/xxx.jpg")
            result.put("sptoken", "33ffffh54444dddd")
            result.put("msg", "success")
            callback.onReceiveValue(result)
        } catch (e: JSONException) {
            e.printStackTrace()
        }

    }

    override fun Logout(jsonObj: JSONObject, callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Logout info = $jsonObj")
        val result = JSONObject()
        try {
            result.put("status", 0)
            result.put("msg", "success")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        callback.onReceiveValue(result)
    }

    override  fun Pay(jsonObj: JSONObject, callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Logout Pay = $jsonObj")
        val result = JSONObject()
        try {
            result.put("status", 0)
            result.put("msg", "success")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        callback.onReceiveValue(result)
    }

    override  fun PushIcon(jsonObj: JSONObject, callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Logout PushIcon = $jsonObj")
        val result = JSONObject()
        try {
            result.put("status", 0)
            result.put("msg", "success")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        callback.onReceiveValue(result)
    }

    override fun Share(jsonObj: JSONObject, callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Logout Share = $jsonObj")
        val result = JSONObject()
        try {
            result.put("status", 0)
            result.put("msg", "success")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        callback.onReceiveValue(result)
    }

    override  fun OpenBBS(jsonObj: JSONObject, callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Logout OpenBBS = $jsonObj")
        val result = JSONObject()
        try {
            result.put("status", 0)
            result.put("msg", "success")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        callback.onReceiveValue(result)
    }

    override fun GetFriendsList(jsonObj: JSONObject,
                       callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Logout GetFriendsList = $jsonObj")
        val result = JSONObject()
        val array = JSONArray()
        try {

            val p1 = JSONObject()
            p1.put("userId", "1111111")
            p1.put("nickName", "xiaoming")
            p1.put("photo", "http://xxx.com/xxx.jpg")
            p1.put("sex", "0")

            val p2 = JSONObject()
            p2.put("userId", "1111111")
            p2.put("nickName", "xiaoming")
            p2.put("photo", "http://xxx.com/xxx.jpg")
            p2.put("sex", "0")
            array.put(p1)
            array.put(p2)

            result.put("status", 0)
            result.put("msg", "success")
            result.put("friends", array)
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        callback.onReceiveValue(result)
    }

    override fun SendMessageToPlatform(jsonObj: JSONObject,
                              callback: ValueCallback<JSONObject>) {
        // TODO Auto-generated method stub
        Log.d(TAG, "Logout SendMessageToPlatform = $jsonObj")
        val result = JSONObject()
        try {
            result.put("status", 0)
            result.put("msg", "success")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        callback.onReceiveValue(result)
    }

}
